package com.x.servlet;

import com.x.bean.User;
import com.x.service.student.StudentService;
import com.x.service.student.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 13925
 */
public class StudentUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User userSession = (User) req.getSession().getAttribute("userSession");
        String code = userSession.getCode();
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User user = new User();
        user.setCode(code);
        user.setName(name);
        user.setPassword(password);
        StudentService studentService = new StudentServiceImpl();
        if (studentService.updateStudent(user)) {
            resp.sendRedirect(req.getContextPath()+"/jsp/frameStudent.jsp");
        } else {
            req.setAttribute("error","修改失败");
            req.getRequestDispatcher("studentupdate.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
